import { message } from "antd";
import { getTeamListInfo } from "../utils/fetchCollections/member/getTeamListInfo";
import { getMemberListInfo } from "../utils/fetchCollections/member/getMemberListInfo";
import { memberInfo } from "../stores/member";
import { getMemberFlipPriceInfo } from "../utils/fetchCollections/member/getMemberFlipPriceInfo";

export  type ISearchOptionsMemberInfo = {
    teamId: number
    memberName: string
    isFavorite: boolean
}

export interface IMemberFlipInfo {
    customs: {
        anonymityCost: number;
        answerType: 1 | 2 | 3;
        baseCost: number;
        normalCost: number;
        privateCost: number;
        status: number
    }[]
}

export class MemberService {
    /**
     * 获取成员列表
     * @returns 成员列表
     */
    static async getMemberListInfo(token: string) {
        if (!token) {
            message.error("请先登录");
            return;
        }
        const { serverTabList } = await getTeamListInfo(token)
        const memberInfoList = []
        for (const serverTabId  of serverTabList) {
            const { serverApiList } = await getMemberListInfo(token, serverTabId?.tabId)
            if (serverApiList) {
                for (const memberInfo of serverApiList) {
                    memberInfoList.push({...memberInfo, teamName: serverTabId?.tabName})
                }
            }
        }
        const memberInfoListForDB: Array<memberInfo> = []
        memberInfoList.forEach(memberInfo => {
            const member = {
                teamName: memberInfo.teamName,
                teamId: memberInfo.teamId,
                serverIcon: memberInfo.serverDefaultIcon,
                serverId: memberInfo.serverId,
                serverName: memberInfo.serverName,
                memberId: memberInfo.serverOwner,
                memberName: memberInfo.serverDefaultName,
            }
            memberInfoListForDB.push(member)
        })
        return memberInfoListForDB
    }
    /**
     * 
     * @param props 根据查询条件，获取成员列表
     */
    static async getSearchOptionsMemberInfo(props: ISearchOptionsMemberInfo, userId: string) {
        const { teamId, memberName, isFavorite } = props
        const result = await chrome.storage.local.get('memberInfo')
        // 其实是个 Record<string, Array<>>, 按 userId获取对应的关注列表
        const favoriteList = await chrome.storage.local.get('favoriteList')
        const favoriteListArray = favoriteList.favoriteList?.[userId] || []
        const memberInfoList: Array<memberInfo> = result.memberInfo || []
        const filteredMemberInfoList = memberInfoList.filter(item => {
            if (teamId && item.teamId !== teamId) {
                return false
            }
            if (memberName && !item.memberName.includes(memberName)) {
                return false
            }
            if (isFavorite && !favoriteListArray.includes(item.memberId)) {
                return false
            }
            return true
        })
        return filteredMemberInfoList
    }

    static async getTeams() {
        const result = await chrome.storage.local.get('memberInfo')
        const memberInfoList: Array<memberInfo> = result.memberInfo || []
        console.log("memberInfoList:", memberInfoList)
        const teamMap = new Map<number, string>()
        memberInfoList.forEach(item => {
            teamMap.set(item.teamId, item.teamName)
        })
        const teamList = Array.from(teamMap.entries()).map(([teamId, teamName]) => ({
            label: teamName,
            value: teamId
        }))
        return teamList
    }
    static async getMemberFlipInfo(memberId: string, token: string) {
        const res = await getMemberFlipPriceInfo(token, memberId) as IMemberFlipInfo
        return res
    }
}